import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文
plt.rcParams['axes.unicode_minus'] = False    # 正常显示负号

# 实验一：复数的基本运算 (简化版)
print("实验一：复数的基本运算")
print("="*40)

# 计算各种根
print("1. 计算各种根:")
sqrt_i = np.sqrt(1j)
print(f"  sqrt(i) = {sqrt_i:.4f}")
sqrt_neg_i = np.sqrt(-1j)
print(f"  sqrt(-i) = {sqrt_neg_i:.4f}")
sqrt_1_plus_i = np.sqrt(1+1j)
print(f"  sqrt(1+i) = {sqrt_1_plus_i:.4f}")
sqrt4_neg1 = (-1+0j)**(1/4)
print(f"  sqrt[4](-1) = {sqrt4_neg1:.4f}")

# 计算复数的模
z1 = -2j * (3+1j) * (2+4j) * (1+1j)
print(f"  |-2i(3+i)(2+4i)(1+i)| = {abs(z1):.4f}")

# 验证模长性质
a = np.exp(1j * np.pi/3)  # |a|=1
b = 0.5 * np.exp(1j * np.pi/6)  # |b|<1
result = abs((a-b) / (1 - np.conj(a)*b))
print(f"  当|a|=1, |b|<1时: |(a-b)/(1-a'bar*b)| = {result:.4f}")

# 验证共轭性质
z = 2 + 3j
expr = z / (z**2 + 1)
z_conj = np.conj(z)
expr_conj = z_conj / (z_conj**2 + 1)
print(f"  z/(z²+1) 与 z̄/(z̄²+1) 共轭: {abs(expr - np.conj(expr_conj)) < 1e-10}")

# 可视化单位根
print("\n2. 可视化单位根:")
n = 6
roots = [np.exp(2j * np.pi * k / n) for k in range(n)]

fig, ax = plt.subplots(1, 1, figsize=(5, 5))
unit_circle = np.exp(1j * np.linspace(0, 2*np.pi, 100))
ax.plot(np.real(unit_circle), np.imag(unit_circle), 'k--', label='单位圆')

for i, root in enumerate(roots):
    ax.plot(np.real(root), np.imag(root), 'ro', markersize=6)
    ax.text(np.real(root)*1.1, np.imag(root)*1.1, f'ω{i}', fontsize=10)

ax.set_xlim(-1.2, 1.2)
ax.set_ylim(-1.2, 1.2)
ax.set_xlabel('实部')
ax.set_ylabel('虚部')
ax.set_title('6次单位根')
ax.grid(True)
plt.axis('equal')
plt.tight_layout()
plt.show()